
/**   
*    
* 项目名称：a_yongan   
* 类名称：CommonController   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月13日 下午9:38:14    
* 修改时间：2018年6月13日 下午9:38:14   
* 修改备注：   
* @version    
*    
*/
package com.yongan.web.util;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.business.entity.Business;
import com.yongan.business.service.BusinessService;
import com.yongan.purchase.entity.Purchase;
import com.yongan.purchase.service.PurchaseService;
import com.yongan.user.entity.Dept;
import com.yongan.user.entity.User;
import com.yongan.user.service.DeptService;
import com.yongan.user.service.UserService;
import com.yongan.util.Tree;
import com.yongan.util.TreeForBusiness;
import com.yongan.util.TreeForGoodsinformation;
import com.yongan.warehouse.entity.GoodType;
import com.yongan.warehouse.entity.Goodsinformation;
import com.yongan.warehouse.entity.Warehouse;
import com.yongan.warehouse.service.GoodTypeService;
import com.yongan.warehouse.service.GoodsinformationService;
import com.yongan.warehouse.service.WarehouseService;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/util")
public class CommonController {

	@Autowired
	private DeptService deptService;
	@Autowired
	private UserService userService;
	@Autowired
	private GoodTypeService goodTypeService;
	@Autowired
	private WarehouseService warehouseService;
	@Autowired
	private BusinessService businessService;
	@Autowired
	private GoodsinformationService goodsinformationService;
	@Autowired
	private PurchaseService purchaseService;
	/**
	 * 返回组织机构树
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/tree", method = RequestMethod.GET)
	public String deptlist(Model model) {
		List<Dept> deptTree = deptService.getDeptListTree();
		JSONArray jsonObject = JSONArray.fromObject(deptTree);
		String json = jsonObject.toString();
		json = json.replaceAll("deptId", "id").replaceAll("topId", "pId")
				.replaceAll("deptName", "name");
		model.addAttribute("menus", json);
		return "checkzTree";
	}
	/**
	 * 返回组织机构和用户地址树
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/usertree", method = RequestMethod.GET)
	public String deptanduserlist(Model model) {
		List<Dept> deptTree = deptService.getDeptListTree();
		List<User> userTree = userService.getUserListTree();
		//JSONArray jsonObject = JSONArray.fromObject(deptTree);
		//JSONArray jsonObject1 = JSONArray.fromObject(userTree);
		
		List<Tree> treeList = new ArrayList<Tree>();
		for (Dept dept:deptTree){
			Tree tree = new Tree();
			tree.setId(String.valueOf(dept.getDeptId()));
			tree.setName(dept.getDeptName());
			tree.setpId(dept.getTopId());
			if(dept.getDeptId().equals("1")){
				tree.setOpen(true);
			}
			tree.setNocheck(true);
			treeList.add(tree);
		}
		for(User user:userTree){
			Tree tree = new Tree();
			tree.setId(String.valueOf(user.getUserId()));
			tree.setName(user.getUserName());
			tree.setpId(user.getDeptId());
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
	
		//System.out.println(json);
		/*json = json.replaceAll("deptId", "id").replaceAll("topId", "pId")
				.replaceAll("deptName", "name");*/
		model.addAttribute("useranddept", json);
		return "checkUserzTree";
	}
	/**
	 * 返回货品类别树
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/goodtypetree", method = RequestMethod.GET)
	public String goodtypelist(Model model) {
		List<GoodType> goodTypeTree = goodTypeService.getAllTree();
		List<Tree> treeList = new ArrayList<Tree>();
		for(GoodType goodtype:goodTypeTree){
			Tree tree = new Tree();
			tree.setId(goodtype.getGoodtypeId());
			tree.setName(goodtype.getGoodtypeMc());
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		model.addAttribute("goodtypeTree", json);
		return "checkGoodTypezTree";
	}
	
	/**
	 * 返回仓库树
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/warehousetree", method = RequestMethod.GET)
	public String warehouselist(Model model) {
		List<Warehouse> warehouseTree = warehouseService.getAllTree();
		List<Tree> treeList = new ArrayList<Tree>();
		for(Warehouse warehouse:warehouseTree){
			Tree tree = new Tree();
			tree.setId(warehouse.getWarehouseId());
			tree.setName(warehouse.getWarehouseCkmc());
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		model.addAttribute("warehousetree", json);
		return "checkWarehousezTree";
	}
	
	
	/**
	 * 返回供应商树
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/businesstree", method = RequestMethod.GET)
	public String businesslist(Model model) {
		List<Business> businessTree = businessService.queryAllTree();
		List<TreeForBusiness> treeList = new ArrayList<TreeForBusiness>();
		for(Business business:businessTree){
			TreeForBusiness tree = new TreeForBusiness();
			tree.setId(business.getBusinessId());
			tree.setName(business.getBusinessDwmc());
			tree.setPeople(business.getBusinessLxr());
			tree.setPhone(business.getBusinessDh());
			if(business.getBusinessDwxz().equals("客户")){
				tree.setpId("0");
			}else if(business.getBusinessDwxz().equals("供应商")){
				tree.setpId("1");
			}else{
				tree.setpId("2");
			}
			treeList.add(tree);
		}
		for(int i=0;i<3;i++){
			TreeForBusiness tree = new TreeForBusiness();
			tree.setId(String.valueOf(i));
			tree.setNocheck(true);
			if(i==0){
				tree.setName("客户");
			}else if(i==1){
				tree.setName("供应商");
			}else{
				tree.setName("其他");
			}
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		
		model.addAttribute("warehousetree", json);
		return "checkGongYingSzTree";
	}
	/**
	 * 返回货品树
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/goodsinformationtree", method = RequestMethod.GET)
	public String goodsinformationlist(Model model) {
		List<GoodType> goodTypeList = goodTypeService.getAllTree();
		List<Goodsinformation> goodsinformationTree = goodsinformationService.getAllGoodsinformationList();
		List<TreeForGoodsinformation> treeList = new ArrayList<TreeForGoodsinformation>();
		for(Goodsinformation goodsinformation:goodsinformationTree){
			TreeForGoodsinformation tree = new TreeForGoodsinformation();
			tree.setId(goodsinformation.getGoodsinformationNumber());
			tree.setName(goodsinformation.getGoodsinformationHpmc());
			tree.setHpxh(goodsinformation.getGoodsinformationHpxh());
			tree.setDw(goodsinformation.getGoodsinformationHpdw());
			tree.setDj(goodsinformation.getGoodsinformationHpdj());
			tree.setpId(goodsinformation.getGoodsinformationHplbid());
			tree.setRid(goodsinformation.getGoodsinformationId());
			tree.setType(goodsinformation.getGoodsinformationHplb());
			treeList.add(tree);
		}
		for(GoodType goodType:goodTypeList){
			TreeForGoodsinformation tree = new TreeForGoodsinformation();
			tree.setName(goodType.getGoodtypeMc());
			tree.setId(goodType.getGoodtypeId());
			tree.setNocheck(true);
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		model.addAttribute("goodinformationTree", json);
		return "checkGoodsinformationzTree";
	}
	/**
	 * 返回采购订货单树
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/purchasetree", method = RequestMethod.GET)
	public String purchaselist(Model model) {
		List<Purchase> purchaseTree = purchaseService.getPurchaseTree();
		List<Tree> treeList = new ArrayList<Tree>();
		for(Purchase purchase:purchaseTree){
			Tree tree = new Tree();
			tree.setId(purchase.getPurchaseId());
			tree.setName(purchase.getPurchaseDdbh());
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		model.addAttribute("menus", json);
		return "checkzTree";
	}
	
	/**
	 * 返回组织机构和用户地址树-新
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getusernew", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String deptanduserlistnew(Model model) {
		List<Dept> deptTree = deptService.getDeptListTree();
		List<User> userTree = userService.getUserListTree();
		List<Tree> treeList = new ArrayList<Tree>();
		for (Dept dept:deptTree){
			Tree tree = new Tree();
			tree.setId(String.valueOf(dept.getDeptId()));
			tree.setName(dept.getDeptName());
			tree.setpId(dept.getTopId());
			if(dept.getDeptId().equals("1")){
				tree.setOpen(true);
			}
			tree.setNocheck(true);
			treeList.add(tree);
		}
		for(User user:userTree){
			Tree tree = new Tree();
			tree.setId(String.valueOf(user.getUserId()));
			tree.setName(user.getUserName());
			tree.setpId(user.getDeptId());
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		
		return json;
	}
}
